package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.system.domain.UserAddress;
import com.ruoyi.system.domain.vo.OrderDataVo;
import com.ruoyi.system.domain.Shop;
import com.ruoyi.system.domain.vo.OrderInfo;
import com.ruoyi.system.mapper.ShopMapper;
import com.ruoyi.system.mapper.UserAddressMapper;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OrdersMapper;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-27
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserAddressMapper addressMapper;
    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Orders selectOrdersById(Long id)
    {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询订单列表
     * 
     * @param orders 订单
     * @return 订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Long[] ids)
    {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long id)
    {
        return ordersMapper.deleteOrdersById(id);
    }

    @Override
    public OrderInfo SaveOrder(Orders order) {
        order.setId(new Date().getTime());
        String orderNo = IdUtil.fastSimpleUUID();
        order.setOrderNo(orderNo);
        if (order.getAddressId()==null){
            UserAddress userAddress = addressMapper.selectOne(new QueryWrapper<UserAddress>().eq("user_id", order.getUserId()).eq("default_flag", 1));
            order.setAddressId(userAddress.getId());
        }
        order.setOrderTime(new Date());
        order.setOrderStatus(1);
        order.setPayMethod(3);
        order.setCreateTime(new Date());
        ordersMapper.insert(order);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNo(orderNo);
        orderInfo.setMoney(order.getTotalAmount());
        orderInfo.setCreateDate(new Date());
        Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("id", order.getShopId()));
        orderInfo.setShopName(shop.getName());
        orderInfo.setCommunity("3楼");
        return orderInfo;
    }

    @Override
    public String pay(String orderNo) {
        Orders order_no = ordersMapper.selectOne(new QueryWrapper<Orders>().eq("order_no", orderNo));
        order_no.setOrderStatus(2);
        order_no.setCheckoutTime(new Date());
        int i = ordersMapper.updateById(order_no);
        Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("id", order_no.getShopId()));
        shop.setMonthSales(shop.getMonthSales()+1);
        shop.setTotalSales(shop.getTotalSales()+1);
        shopMapper.updateById(shop);
        if (i>0){
            return "支付成功";
        }else{
            return "支付失败";
        }
    }

    @Override
    public List<OrderDataVo> orderList(Long userId) {
        List<Orders> user_id = ordersMapper.selectList(new QueryWrapper<Orders>().eq("user_id", userId));
        List<OrderDataVo> orderDataVoList = new ArrayList<>();
        for (Orders orders : user_id) {
            OrderDataVo orderDataVo = new OrderDataVo();
            Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("id", orders.getShopId()));
            orderDataVo.setId(orders.getOrderNo());
            orderDataVo.setAvatar("http://localhost:8000/dev-api"+shop.getLogoImg());
            orderDataVo.setCommunity("3楼");
            orderDataVo.setStatus(orders.getOrderStatus());
            orderDataVo.setTag("../../static/img/index/订单.png");
            orderDataVo.setCreateTime(DateUtil.format(orders.getOrderTime(),"yyyy-MM-dd HH:mm:ss"));
            orderDataVo.setTotal(orders.getTotalAmount());
            orderDataVo.setShopId(orders.getShopId());
            orderDataVo.setShopName(shop.getName());
            orderDataVoList.add(orderDataVo);
        }
        return orderDataVoList;

    }

    @Override
    public List<OrderDataVo> orderListtoPay(Long userId) {
        List<Orders> user_id = ordersMapper.selectList(new QueryWrapper<Orders>().eq("user_id", userId).eq("order_status",1));
        List<OrderDataVo> orderDataVoList = new ArrayList<>();
        for (Orders orders : user_id) {
            OrderDataVo orderDataVo = new OrderDataVo();
            Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("id", orders.getShopId()));
            orderDataVo.setId(orders.getOrderNo());
            orderDataVo.setAvatar("http://localhost:8000/dev-api"+shop.getLogoImg());
            orderDataVo.setCommunity("3楼");
            orderDataVo.setStatus(orders.getOrderStatus());
            orderDataVo.setTag("../../static/img/index/订单.png");
            orderDataVo.setCreateTime(DateUtil.format(orders.getOrderTime(),"yyyy-MM-dd HH:mm:ss"));
            orderDataVo.setTotal(orders.getTotalAmount());
            orderDataVo.setShopId(orders.getShopId());
            orderDataVo.setShopName(shop.getName());
            orderDataVoList.add(orderDataVo);
        }
        return orderDataVoList;

    }

    @Override
    public List<OrderDataVo> orderListToComment(Long userId) {
        List<Orders> user_id = ordersMapper.selectList(new QueryWrapper<Orders>().eq("user_id", userId).eq("order_status",2));
        List<OrderDataVo> orderDataVoList = new ArrayList<>();
        for (Orders orders : user_id) {
            OrderDataVo orderDataVo = new OrderDataVo();
            Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("id", orders.getShopId()));
            orderDataVo.setId(orders.getOrderNo());
            orderDataVo.setAvatar("http://localhost:8000/dev-api"+shop.getLogoImg());
            orderDataVo.setCommunity("3楼");
            orderDataVo.setStatus(orders.getOrderStatus());
            orderDataVo.setTag("../../static/img/index/订单.png");
            orderDataVo.setCreateTime(DateUtil.format(orders.getOrderTime(),"yyyy-MM-dd HH:mm:ss"));
            orderDataVo.setTotal(orders.getTotalAmount());
            orderDataVo.setShopId(orders.getShopId());
            orderDataVo.setShopName(shop.getName());
            orderDataVoList.add(orderDataVo);
        }
        return orderDataVoList;
    }

    @Override
    public List<OrderDataVo> orderListToDone(Long userId) {
        List<Orders> user_id = ordersMapper.selectList(new QueryWrapper<Orders>().eq("user_id", userId).eq("order_status",3));
        List<OrderDataVo> orderDataVoList = new ArrayList<>();
        for (Orders orders : user_id) {
            OrderDataVo orderDataVo = new OrderDataVo();
            Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("id", orders.getShopId()));
            orderDataVo.setId(orders.getOrderNo());
            orderDataVo.setAvatar("http://localhost:8000/dev-api"+shop.getLogoImg());
            orderDataVo.setCommunity("3楼");
            orderDataVo.setStatus(orders.getOrderStatus());
            orderDataVo.setTag("../../static/img/index/订单.png");
            orderDataVo.setCreateTime(DateUtil.format(orders.getOrderTime(),"yyyy-MM-dd HH:mm:ss"));
            orderDataVo.setTotal(orders.getTotalAmount());
            orderDataVo.setShopId(orders.getShopId());
            orderDataVo.setShopName(shop.getName());
            orderDataVoList.add(orderDataVo);
        }
        return orderDataVoList;    }

    @Override
    public List<OrderDataVo> orderListToRefund(Long userId) {
        List<Orders> user_id = ordersMapper.selectList(new QueryWrapper<Orders>()
                .eq("user_id", userId)
                .eq("order_status",4)
                .or()
                .eq("order_status",5));
        List<OrderDataVo> orderDataVoList = new ArrayList<>();
        for (Orders orders : user_id) {
            OrderDataVo orderDataVo = new OrderDataVo();
            Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("id", orders.getShopId()));
            orderDataVo.setId(orders.getOrderNo());
            orderDataVo.setAvatar("http://localhost:8000/dev-api"+shop.getLogoImg());
            orderDataVo.setCommunity("3楼");
            orderDataVo.setStatus(orders.getOrderStatus());
            orderDataVo.setTag("../../static/img/index/订单.png");
            orderDataVo.setCreateTime(DateUtil.format(orders.getOrderTime(),"yyyy-MM-dd HH:mm:ss"));
            orderDataVo.setTotal(orders.getTotalAmount());
            orderDataVo.setShopId(orders.getShopId());
            orderDataVo.setShopName(shop.getName());
            orderDataVoList.add(orderDataVo);
        }
        return orderDataVoList;
    }

    @Override
    public OrderInfo getOrderInfo(String orderNo) {

        Orders order_no = ordersMapper.selectOne(new QueryWrapper<Orders>().eq("order_no", orderNo));
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNo(orderNo);
        orderInfo.setCommunity(order_no.getConsigneeAddress());
        orderInfo.setMoney(order_no.getTotalAmount());
        orderInfo.setCreateDate(order_no.getOrderTime());
        Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("id", order_no.getShopId()));
        orderInfo.setShopName(shop.getName());
        return orderInfo;
    }

    @Override
    public String refund(String orderNo) {
        Orders order_no = ordersMapper.selectOne(new QueryWrapper<Orders>().eq("order_no", orderNo));
        order_no.setOrderStatus(4);
        int i = ordersMapper.updateById(order_no);
        if (i>0){
            return "退款中";
        }else{
            return "退款失败";
        }
    }
}
